<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no"/>
    <title>动态路由切换</title>
    <style>
        [v-cloak] {
            display: none;
        }
    </style>
</head>
<body>
<div id="app" v-cloak></div>
<script src="js/vue.js"></script>
<script>
    //Input组件
    let InputComponent = {
        template: `
          <div>
            <input type="text" @input="changeValue($event)">
          </div>
        `,
        data() {
            return {}
        },
        props: {
            value: {
                type: String
            }
        },
        model: {
            prop: "value",
            event: "change"
        },
        methods: {
            changeValue(e) {
                // console.log(e);
                this.$emit("change", e.target.value);
            }
        }
    };

    //Button组件
    let ButtonComponent = {
        template: `
            <button type="button" @click="handleClick()"><slot></slot></button>
        `,
        methods: {
            handleClick() {
                this.$emit("click")
            }
        }
    }

    //父组件
    let ParentComponent = {
        template: `
            <div>
                <component :is="componentName" @click="del()" v-model="value">删除</component>
                {{value}}<br/>
                <button type="button" @click="componentName='ButtonComponent'">显示Button组件</button>
                <button type="button" @click="componentName='InputComponent'">显示Input组件</button>
            </div>
        `,
        data() {
            return {
                value: "",
                componentName: "ButtonComponent"
            }
        },
        created() {

        },
        components: {
            InputComponent,
            ButtonComponent
        },
        methods: {
            del() {
                alert('删除');
            }
        }
    };
    //入口组件
    let App = {
        template: `
            <div>
                <ParentComponent></ParentComponent>
            </div>
        `,
        //局部组件注册方式
        components: {
            ParentComponent
        }
    };
    new Vue({
        el: "#app",
        template: `
            <App></App>
        `,
        components: {
            App
        }
    })
</script>
</body>
</html>
